package com.paimeng.utils;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

/**
 * @author: 潘辉
 * @Title: Highlight
 * @ProjectName: paimeng
 * @Description: 关键词高亮显示
 * @date: 2021/6/16 20:01
 */
public class Highlight {
    public static Highlighter getHighligth(Query query){
        // 格式化器
        SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(
                "<span style='color:red'>", "</span>");
        // 准备高亮工具
        Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
        //fragmentSize 结果的长度（不含html标签长度）
        highlighter.setTextFragmenter(new SimpleFragmenter(200));
        return highlighter;
    }
}
